<template>
  <div class="wrapper">
    <FormPanel
      ref="formPanel"
      :list="inputs"
      labelWidth="150"
      :formType="type"
      :isSubmitBtn="false"
      :cols="2"
      @formChange="formChangeHandle"
    />

    <FilterTable
      ref="table"
      height="400"
      :columns="columns"
      :isSelectColumn="false"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'records'"
      :onColumnsChange="columns => this.columns = columns"
      columnsRef="projectOperating_material_departmentReview_info_01"
    ></FilterTable>

    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDialog">关 闭</el-button>
      <el-button
        :loading="saveLoading"
        v-if="this.type == 'edit'"
        type="primary"
        @click="saveDialog"
      >审核通过</el-button>
      <el-button
        :loading="sumbitLoading"
        v-if="this.type == 'edit'"
        type="primary"
        @click="sumbitDialog"
      >审核驳回</el-button>
    </div>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { formatdateByTime } from '@/utils/index';
import { updateDealerMaterial, saveLog, update, getList } from '@/api/cms-api/projectOperating/program/push';
export default {
  mixins: [dictionary],
  components: {},
  props: {
    type: {
      type: String,
      required: true
    },
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  mounted() {
    // console.log(this.value);
  },
  data() {
    return {
      inputs: this.getInputs(),
      inputsData: {},
      formValue: {},
      dataParams: {}, //父类参数
      caseUrlList: [],
      saveLoading: false,
      sumbitLoading: false,
      columns: this.creatHeader(),
      fetchApi: getList, //ipa 接口
      fetchParams: { materialId: this.rowData.materialId, dealerShortname: this.rowData.dealerShortname } //参数
    };
  },
  created() {
    this.init();
  },
  methods: {
    //table表头和数据源
    creatHeader() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        { title: '项目审核部门', dataIndex: 'deptName', sorter: true },
        { title: '项目审核人', dataIndex: 'reviewPersionName', sorter: true },
        { title: '经销商简称', dataIndex: 'dealerShortname', sorter: true },
        {
          title: '审核状态',
          dataIndex: 'reviewStatus',
          sorter: true,
          render: props => {
            //编码转汉字
            return <span>{this.createDictText(props.row.reviewStatus, '2527')}</span>;
          }
        },
        {
          title: '审核时间',
          dataIndex: 'reviewDate',
          sorter: true,
          render: props => {
            return <span>{formatdateByTime(props.row.reviewDate)} </span>;
          }
        },
        { title: '审核意见', dataIndex: 'overruleReason', sorter: true }
      ];
    },
    //审核通过
    async saveDialog() {
      this.$refs.formPanel.SUBMIT_FORM();
      if (null == this.$refs.formPanel.SUBMIT_FORM()) {
        return;
      }
      if (this.formValue !== null) {
        this.saveLoading = true;
        if (this.type === 'edit') {
          let result = {};
          if (this.formValue.caseUrl != null) {
            for (let i = 0; i < this.formValue.caseUrl.length; i++) {
              this.caseUrlList.push(Object.assign({}, { name: this.formValue.caseUrl[i].name }, { url: this.formValue.caseUrl[i].url }));
            }
            this.formValue = Object.assign({}, { fileUrl: this.caseUrlList }, this.formValue);
          }
          this.formValue = Object.assign({}, { materialId: this.rowData.materialId }, this.formValue);
          // this.formValue = Object.assign({}, { dealerId: this.rowData.dealerId }, this.formValue);
          this.formValue = Object.assign({}, this.formValue, { feedbackStatus: 25271003 });
          this.formValue = Object.assign({}, this.formValue, { sourceDept: this.rowData.sourceDept });
          this.formValue = Object.assign({}, { dealerCode: this.rowData.dealerCode }, this.formValue);
          //经销商简称
          this.formValue = Object.assign({}, { dealerShortname: this.rowData.dealerShortname }, this.formValue);
          //修改营销素材反馈审核状态
          // result = await updateDealerMaterial(this.formValue, this.rowData.id);
          result = await update(this.formValue, this.rowData.id);
          //新增营销素材反馈审核日志
          // let resLog = {};
          //审核状态
          // this.formValue = Object.assign({}, { reviewStatus: 25271003 }, this.formValue);
          // resLog = await saveLog(this.formValue);
          if (result.resultCode === 200) {
            this.saveLoading = false;
            this.$notify.success({ title: '提示信息', message: '审核通过!' });
            this.closeDialog('refresh');
          }
        }
      }
    },
    //审核驳回
    async sumbitDialog() {
      this.$refs.formPanel.SUBMIT_FORM();
      if (null == this.$refs.formPanel.SUBMIT_FORM()) {
        return;
      }
      if (this.formValue !== null && this.formValue.overruleReason != null) {
        this.sumbitLoading = true;
        if (this.type === 'edit') {
          let result = {};
          if (this.formValue.caseUrl != null) {
            for (let i = 0; i < this.formValue.caseUrl.length; i++) {
              this.caseUrlList.push(Object.assign({}, { name: this.formValue.caseUrl[i].name }, { url: this.formValue.caseUrl[i].url }));
            }
            this.formValue = Object.assign({}, { fileUrl: this.caseUrlList }, this.formValue);
          }
          this.formValue = Object.assign({}, { materialId: this.rowData.materialId }, this.formValue);
          // this.formValue = Object.assign({}, { dealerId: this.rowData.dealerId }, this.formValue);
          this.formValue = Object.assign({}, this.formValue, { feedbackStatus: 25271004 });
          this.formValue = Object.assign({}, this.formValue, { sourceDept: this.rowData.sourceDept });
          this.formValue = Object.assign({}, { dealerCode: this.rowData.dealerCode }, this.formValue);
          //经销商简称
          this.formValue = Object.assign({}, { dealerShortname: this.rowData.dealerShortname }, this.formValue);
          // result = await updateDealerMaterial(this.formValue, this.rowData.id);
          result = await update(this.formValue, this.rowData.id);
          //let resLog = {};
          //审核状态
          // this.formValue = Object.assign({}, { reviewStatus: 25271004 }, this.formValue);
          // resLog = await saveLog(this.formValue);
          if (result.resultCode === 200) {
            this.sumbitLoading = false;
            this.$notify.success({ title: '提示信息', message: '审核驳回!' });
            this.closeDialog('refresh');
          }
        }
      } else {
        this.$notify.warning({ title: '提示信息', message: '审核意见非空！且字符数量限制在300！' });
      }
    },
    formatDates: function(value) {
      // 这里的 value 就是需要过滤的数据
      if (value == null || value == '') {
        return '';
      }
      var date = new Date(value);
      var year = date.getFullYear();
      var month = date.getMonth() + 1;
      var day = date.getDate();
      return year + '-' + month + '-' + day;
    },
    init() {
      if (this.type === 'edit') {
        this.inputs.forEach(x => {
          x.initialValue = this.rowData[x.fieldName];
        });
      }
      if (this.type === 'show') {
        this.materialId = this.rowData.materialId;
        this.materialStatus = this.rowData.materialStatus;
        this.inputs.forEach(x => {
          x.initialValue = this.rowData[x.fieldName];
        });
      }
    },
    formChangeHandle(val) {
      this.formValue = val;
    },
    getInputs() {
      return [
        {
          type: 'INPUT',
          label: '反馈编号',
          disabled: true,
          fieldName: 'feedbackCode',
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: '事业部',
          disabled: true,
          fieldName: 'orgName',
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: '来源部门',
          disabled: true,
          fieldName: 'deptName',
          initialValue: ''
        },
        {
          type: 'SELECT',
          label: '反馈状态',
          disabled: true,
          fieldName: 'feedbackStatus',
          placeholder: '请选择...',
          initialValue: '',
          itemList: this.createDictList('2527')
        },
        {
          type: 'INPUT',
          label: '经销商代码',
          disabled: true,
          fieldName: 'dealerCode',
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: '经销商名称',
          disabled: true,
          fieldName: 'dealerName',
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: '营销素材代码',
          disabled: true,
          fieldName: 'materialCode',
          placeholder: '系统自动生成',
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: '营销素材名称',
          disabled: true,
          fieldName: 'materialName',
          placeholder: '请输入...',
          initialValue: '',
          rules: [{ required: true, message: '请输入营销素材名称', trigger: 'blur' }]
        },
        {
          type: 'DATE',
          label: '反馈截止日期',
          disabled: true,
          fieldName: 'feedbackEndDate',
          valueFormat: 'yyyy-MM-dd',
          placeholder: '请选择...',
          itemList: []
        },
        {
          type: 'INPUT',
          label: '营销素材类型-一级',
          disabled: true,
          fieldName: 'materialTypeOneName',
          initialValue: '',
          placeholder: ''
        },
        {
          type: 'INPUT',
          label: '营销素材类型-二级',
          disabled: true,
          fieldName: 'materialTypeTwoName',
          initialValue: '',
          placeholder: ''
        },
        {
          type: 'INPUT',
          label: '营销素材类型-三级',
          disabled: true,
          fieldName: 'materialTypeThreeName',
          initialValue: '',
          placeholder: ''
        },
        {
          type: 'INPUT',
          label: '营销素材类型-四级',
          disabled: true,
          fieldName: 'materialTypeFourName',
          initialValue: '',
          placeholder: ''
        },
        {
          type: 'INPUT',
          label: '营销素材类型-五级',
          disabled: true,
          fieldName: 'materialTypeFiveName',
          initialValue: '',
          placeholder: ''
        },
        {
          type: 'SELECT',
          label: '反馈结果',
          fieldName: 'feedbackResult',
          initialValue: '',
          disabled: true,
          itemList: this.createDictList('2575')
        },
        {
          type: 'INPUT',
          label: '反馈描述',
          disabled: true,
          fieldName: 'feedbackDesc',
          initialValue: '',
          selfCols: 2,
          placeholder: ''
        },
        {
          type: 'UPLOAD_FILE',
          label: '附件',
          disabled: true,
          fieldName: 'caseUrl',
          selfCols: 2,
          upload: {
            actionUrl: '/api/file/oss/upload'
          }
        },
        {
          type: 'TEXT_AREA',
          label: '审核意见',
          fieldName: 'overruleReason',
          initialValue: '',
          selfCols: 2,
          maxlength: 300
        }
      ];
    },
    changeHandle(data) {
      this.inputsData = data;
      this.$emit('close', data);
    },
    //取消操作
    closeDialog(param) {
      this.$emit('close', false, param);
    }
  }
};
</script>
<style scoped>
.wrapper {
  height: calc(100vh - 110px);
}
</style>
